import {
  Component,
  ViewChild,
  Injector,
  Output,
  EventEmitter,
  ElementRef,
} from "@angular/core";
import { ModalDirective } from "ngx-bootstrap";
import { AppComponentBase } from "@shared/common/app-component-base";

import * as moment from "moment";
import { finalize } from "rxjs/operators";
import {
  MainTaskServiceProxy,
  TaskDto,
  MainTaskGoProductionDto,
  NameValueDtoOfNameValueDto,
  PipelineServiceProxy,
  SideTaskServiceProxy,
  MonthPlanServiceProxy,
  TechDocServiceProxy,
  SimpleTechDocListDto,
  TechDocDto,
} from "@shared/service-proxies/service-proxies";
import { LazyLoadEvent } from "primeng/api";

@Component({
  selector: "boundReadytechdoc",
  templateUrl: "./bound-readytechdoc-modal.component.html",
  styleUrls: ["./bound-readytechdoc-modal.component.less"],
})
export class BoundReadytechdocModalComponent extends AppComponentBase {
  @ViewChild("actionModal") modal: ModalDirective;
  @ViewChild("tmplCombobox") tmplCombobox: ElementRef;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
  active = false;
  saving = false;
  monthId: string;
  techDocCode: string;
  drawingCode: string;
  event?: LazyLoadEvent;
  techDocs = new Array<SimpleTechDocListDto>();
  selectedTechDoc = new SimpleTechDocListDto();
  filterText = "";
  techDocView = new TechDocDto();
  oldTechDocView = new TechDocDto();
  constructor(
    injector: Injector,
    private _monthServiceProxy: MonthPlanServiceProxy,
    private _techDocServiceProxy: TechDocServiceProxy
  ) {
    super(injector);
  }
  show(
    id: string,
    techDocCode: string,
    drawingCode: string,
    event?: LazyLoadEvent
  ): void {
    this.active = true;
    this.monthId = id;
    this.techDocCode = techDocCode;
    this.drawingCode = drawingCode;
    this.event = event;
    if (techDocCode != null) {
      this._techDocServiceProxy.getTechDoc(techDocCode).subscribe((result1) => {
        this.oldTechDocView = result1;
        this.techDocView = result1;
        if (result1.codeId) {
          this.selectedTechDoc = this.techDocs.filter((el) => {
            return el.codeId === result1.codeId;
          })[0];
        } else {
          this.selectedTechDoc = new SimpleTechDocListDto();
        }
      });
    }
    if (this.filterText === "") {
      this.filterText = this.drawingCode;
    }
    this._techDocServiceProxy
      .getSimpleTechDocsByRef(this.filterText)
      .subscribe((result) => {
        this.techDocs = result.items;
      });

    this.modal.show();
  }
  rowSelect() {
    this.primengTableHelper.showLoadingIndicator();
    let rowData = this.selectedTechDoc;
    this._techDocServiceProxy.getTechDoc(rowData.codeId).subscribe((result) => {
      this.techDocView = result;
      this.primengTableHelper.hideLoadingIndicator();
    });
  }
  save(): void {
    this.saving = true;
    this._monthServiceProxy
      .boundMonthTechDoc(this.monthId, this.selectedTechDoc.codeId)
      .pipe(
        finalize(() => {
          this.saving = false;
        })
      )
      .subscribe((result) => {
        this.notify.success("绑定成功");
        this.close();
        this.modalSave.emit(this.event);
      });
  }

  close(): void {
    this.saving = false;
    this.modal.hide();
  }
}
